package com.yln.watcher.client;

import com.yln.watcher.WorkersChildrenWatcher;
import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.Stat;

import java.util.List;

public class CreateWorkersNodeCallback implements AsyncCallback.Create2Callback {

    @Override
    public void processResult(int rc, String path, Object ctx, String name, Stat stat) {
        ZkClient client = (ZkClient) ctx;
        KeeperException.Code code = KeeperException.Code.get(rc);
        switch (code) {
            case OK:
                // 创建/workers成功 做一些初始化/workers的工作
                client.addWatcher(path, new WorkersChildrenWatcher(client));
                break;
            case NODEEXISTS:
                // /workers节点已经存在
                // 获取所有workers 并加入缓存
                List<String> allWorkers = client.findAllWorkers();
                client.cacheWorkers(allWorkers);
                client.addWatcher(path, new WorkersChildrenWatcher(client));
                break;
            default:
                ;

        }
    }

}
